// src/main/java/com/qst/itoffer/service/UserService.java
package com.qst.itoffer.service;

import com.qst.itoffer.entity.PageBean;
import com.qst.itoffer.entity.User;
import com.qst.itoffer.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    public void saveUser(User user) {
        userMapper.insertUser(user);
    }

    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getUserPwd().equals(password)) {
            return user;
        }
        return null;
    }

    public void updateUserStatus(Long userId, Integer status) {
        userMapper.updateUserStatus(userId, status);
    }

    //用户分页查询
    public PageBean<User> getUsersByPage(int pageNo, int pageSize) {
        int totalRecords = userMapper.selectCount();
        int totalPages = (totalRecords + pageSize - 1) / pageSize;
        int start = (pageNo - 1) * pageSize;

        List<User> users = userMapper.selectPage(start, pageSize);

        return new PageBean<>(pageNo, pageSize, totalRecords, totalPages, users);
    }
    //查询用户ByID
    public User getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    //跟新用户
    public void updateUser(User user) {
        userMapper.update(user);
    }

    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }

    public void deleteUser(Integer userId) {
        userMapper.deleteUser(userId);
    }
}